import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front';

export default function getConTableData(isRefresh) {
    const {orgVal, searchModalValue, contractPageInfo, queryOid} = this.state;
    const {table} = this.props;
    if (!orgVal.refpk) {
        table.setAllTableData('contractList', {
            rows: []
        });
        this.updateButtonStatus();
        return;
    }
    //if (!queryOid) return;

    return proFetch({
        url: '/nccloud/hrcm/account/QueryContractAction.do',
        body: {
            ...searchModalValue,
            pk_org: orgVal.refpk,
            areaCode: 'contractList',
            pageInfo: contractPageInfo,
            isContract: true,
            oid: queryOid
        },
    }).then((res) => {
        if (res.success) {
            if (isRefresh) {
                toast({color: "success", content: this.state.json['cm6011-000099']});
            }
            if (res.data && res.data.data && res.data.data.contractList) {
                table.setAllTableData('contractList', {
                    rows: res.data.data.contractList.rows
                });
                if (res.data.data.contractList.rows.length) {
                    this.props.table.setClickRowIndex('contractList', {
                        record: res.data.data.contractList.rows[0].values,
                        index: 0
                    });
                    this.props.table.focusRowByIndex('contractList', 0);
                }
                this.setState({
                    searched: true,
                    pk_psndocList: res.data.pk_psndocList,
                    contractPageInfo: res.data.data.contractList.pageInfo || {
                        pageSize: 10,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    }
                }, () => {
                    this.updateButtonStatus();
                });
            } else {
                table.setAllTableData('contractList', {rows: []});
                this.setState({
                    searched: true,
                    pk_psndocList: [],
                    contractPageInfo: {
                        pageSize: 10,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    }
                }, () => {
                    this.updateButtonStatus();
                });
            }
        }
    });
}
